package com.adam.management.controller;

import com.adam.management.dto.SysMenuDTO;
import com.adam.management.entity.SysMenu;
import com.adam.management.response.ListModel;
import com.adam.management.response.ResponseData;
import com.adam.management.service.SysMenuService;
import com.adam.management.service.impl.SysMenuServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @projectName: adam-management-system
 * @description:
 * @author: Adam
 * @create: 2019-10-16 13:20
 **/

@Api(value = "系统管理菜单相关接口",tags = "系统管理菜单相关接口")
@Controller
@RequestMapping("/menu")
public class SysMenuController {

    private static Logger logger = LoggerFactory.getLogger(SysMenuServiceImpl.class);

    @Autowired
    private SysMenuService sysMenuService;


    @ApiOperation(value = "菜单页面")
   /* @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "productIds", value = "商品ID,多个以逗号隔开", required = true)
    })*/
    @RequestMapping(value = "/management",method = RequestMethod.GET)
    public String menuPage(){
        return "menu";
    }


    @ApiOperation(value = "增加菜单")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String menuAdd(@RequestBody SysMenuDTO sysMenuDTO){
        return JSON.toJSONString(sysMenuService.addMenu(sysMenuDTO));
    }

    @ApiOperation(value = "获取菜单树",response = SysMenu.class)
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String getSysMenu(){
        return JSON.toJSONString(sysMenuService.getMenusWithPermission(null));
    }


    @ApiOperation(value = "获取没有菜单树结构的所有菜单",response = SysMenu.class)
    @RequestMapping(value = "/get/original",method = RequestMethod.GET)
    @ResponseBody
    public String getOriginalSysMenu(){
        List<SysMenuDTO> sysMenuDTOS = sysMenuService.getMenusWithSorted(null);
        ListModel<SysMenuDTO> listModel = new ListModel<>(sysMenuDTOS);
        return JSON.toJSONString(ResponseData.success(listModel));
    }


    @ApiOperation(value = "编辑菜单",response = ResponseData.class)
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public String editSysMenu(SysMenuDTO sysMenuDTO){
        return JSON.toJSONString(sysMenuService.editMenuByPrimaryKey(sysMenuDTO));
    }


    @ApiOperation(value = "删除菜单",response = ResponseData.class)
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public String deleteMenu(@RequestBody String param){
        //post请求中，没有对象接收json时，可以将json转化为字符串来接收再解析
        if (StringUtils.isBlank(param)){
            return JSON.toJSONString(ResponseData.fail("请选择要删除的记录"));
        }
        JSONObject idsJson = JSON.parseObject(param);
        String[] idsArr = idsJson.getString("ids").split(",");
        List<String> idsList = Arrays.asList(idsArr);
        return JSON.toJSONString(sysMenuService.deleteMenu(idsList));
    }

}
